package org.example

import scala.io.Source

object scalafunData1 {
  def main(args: Array[String]): Unit = {
    def func1: Int => Int = (i: Int) => i * 2

    val list1 = List(1, 2, 3, 4, 5).map(func1)
    println(list1)
    val s = "scala"
    println(s.toUpperCase)


    val func2 = (word: String) => word.toUpperCase
    val wordlist = List("spark", "scala", "java", "python")
    val result = wordlist.map(func2)
    //
    //val result2=wordlist.map(word=>word.toUpperCase)
    //当参数只出现一次，参数也可省略
    val result1 = wordlist.map(_.toUpperCase)
    println(result)
    println(result1)
    val numList = List(20, 50, 70)
    numList.map(t => t + 5).foreach(println)
    numList.foreach(t => println(t * 2))
    //数组操作常用方法
    println(numList.head)
    println(numList.tail)
    println(numList.max)


    val exam = Array(("张三", Array(58, 69, 72, 88)), ("李四", Array(49, 66, 77, 88)))
    exam.map(tp => (tp._1, tp._2.max)).foreach(println)
    println("--------------")
    //求张三和李四的及格和平均数scala语法，简化写法
    exam.map(tp => (tp._1, tp._2.map(t => if (t >= 60) 1 else 0).sum)).foreach(println)
    exam.map(tp => (tp._1, tp._2.count(t => t >= 60))).foreach(println)
    exam.map(tp => (tp._1, tp._2.count(_ >= 60))).foreach(println)


    exam.map(tp => (tp._1, tp._2.sum * 1.0 / tp._2.length)).foreach(println)


    //求张三和李四的及格和平均数
    exam.foreach { case (name, score) => var passCount = 0
      var totalScore = 0
      score.foreach { score =>
        if (score >= 60) {
          passCount += 1
        }
        totalScore += score
      }
      val averageScore = totalScore.toDouble / score.length
      println(s"$name,及格门数：$passCount,平均数：$averageScore")
    }
    //去掉最低分和最高分的平均数
    //val scores  = Array(("张三", Array(58, 69, 72,88 ,90,100)), ("李四", Array(49, 66, 77, 88,92,99)))


    //filter过滤 类型和元素不变，筛选出需要的元素

    //    val arr1=Array(1,2,3,4,5,6)
    //    arr1.filter(t=>if(t%2==0)true else  false).foreach(println)
    //    arr1.filter(t=>t%2==0).foreach(println)
    //    arr1.filter(_%2==0).foreach(println)
    //flatten将二维压缩成一维。叫扁平化
    val arr2 = Array(Array(1, 2, 3), Array(3, 6, 9))
    arr2.flatten.foreach(println)
    val arr3 = Array("a b c", "d e f")
    //flatMap 扁平映射
    arr3.flatMap(t => t.split("")).foreach(println)
    val arr4 = Array(("a", "b"), ("c", "d"))
    arr4.flatMap(t => Array(t._1, t._2)).foreach(println)
    //降维
    val arr5 = Array("张三 60 70 80", "李四 50 70 90")
    arr5.flatMap(t => t.split("")).foreach(println)
    println(arr5.head)
    //arr5.flatMap(t=>t.split(" ")).head.foreach(print)

    arr5.map(t => t.split(" ")).flatMap((arr5 => arr5.tail.map(t => (arr5.head, t)))).foreach(println)
    //将一组字符串进行分词，并保存单词列表
    val wordArr=List("hello scala","heloo python","hello java")
    val wordSpilt=wordArr.map(_.split(" "))//分词
    val wordFlatten=wordSpilt.flatten//扁平
    println(wordFlatten)
    val wordFlatMap=wordArr.flatMap(_.split(""))
    print(wordFlatten)


  }


}
